package net.shopxx.entity;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import net.shopxx.bean.SpecificationValue;
import net.shopxx.util.SerialNumberUtil;
import net.shopxx.util.SystemConfigUtil;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.ForeignKey;

@Entity
public class Goods extends BaseEntity {
	private static final long serialVersionUID = -4663151563624172169L;
	private String goodsSn;
	private String name;
	private BigDecimal price;
	private BigDecimal cost;
	private BigDecimal marketPrice;
	private Integer weight;
	private Integer store;
	private Integer freezeStore;
	private String storePlace;
	private Boolean isMarketable;
	private Boolean isDefault;
	private String specificationValueStore;
	private Product product; //产品
	private Set<Specification> specificationSet;
	private Set<CartItem> cartItemSet;// 购物车项
	private Set<OrderItem> orderItemSet;// 订单项
	private Set<DeliveryItem> deliveryItemSet;// 物流项

	// private Set<GoodsNotify> lIlIlllII = new HashSet(); //将来做评论用

	@Column(nullable = false, unique = true)
	/**
	 * @return the goodsSn
	 */
	public String getGoodsSn() {
		return goodsSn;
	}

	/**
	 * @param goodsSn
	 *            the goodsSn to set
	 */
	public void setGoodsSn(String goodsSn) {
		this.goodsSn = goodsSn;
	}

	@Column(nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String paramString) {
		this.name = paramString;
	}

	@Column(nullable = false, precision = 15, scale = 5)
	public BigDecimal getPrice() {
		return this.price;
	}

	public void setPrice(BigDecimal paramBigDecimal) {
		this.price = SystemConfigUtil.getPriceScaleBigDecimal(paramBigDecimal);
	}

	@Column(precision = 15, scale = 5)
	public BigDecimal getCost() {
		return this.cost;
	}

	public void setCost(BigDecimal paramBigDecimal) {
		this.cost = SystemConfigUtil.getPriceScaleBigDecimal(paramBigDecimal);
	}

	@Column(nullable = false, precision = 15, scale = 5)
	public BigDecimal getMarketPrice() {
		return this.marketPrice;
	}

	public void setMarketPrice(BigDecimal paramBigDecimal) {
		this.marketPrice = SystemConfigUtil
				.getPriceScaleBigDecimal(paramBigDecimal);
	}

	@Column(nullable = false)
	public Integer getWeight() {
		return this.weight;
	}

	public void setWeight(Integer paramInteger) {
		this.weight = paramInteger;
	}

	public Integer getStore() {
		return this.store;
	}

	public void setStore(Integer paramInteger) {
		this.store = paramInteger;
	}

	@Column(nullable = false)
	public Integer getFreezeStore() {
		return this.freezeStore;
	}

	public void setFreezeStore(Integer paramInteger) {
		this.freezeStore = paramInteger;
	}

	public String getStorePlace() {
		return this.storePlace;
	}

	public void setStorePlace(String paramString) {
		this.storePlace = paramString;
	}

	@Column(nullable = false)
	public Boolean getIsMarketable() {
		return this.isMarketable;
	}

	public void setIsMarketable(Boolean paramBoolean) {
		this.isMarketable = paramBoolean;
	}

	@Column(nullable = false)
	public Boolean getIsDefault() {
		return this.isDefault;
	}

	public void setIsDefault(Boolean paramBoolean) {
		this.isDefault = paramBoolean;
	}

	@ManyToOne(fetch = FetchType.LAZY)

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	@Column(length = 3000)
	public String getSpecificationValueStore() {
		return this.specificationValueStore;
	}

	public void setSpecificationValueStore(String paramString) {
		this.specificationValueStore = paramString;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "goods")
	@Cascade(value = { CascadeType.DELETE })
	public Set<CartItem> getCartItemSet() {
		return cartItemSet;
	}

	public void setCartItemSet(Set<CartItem> cartItemSet) {
		this.cartItemSet = cartItemSet;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "goods")
	public Set<OrderItem> getOrderItemSet() {
		return orderItemSet;
	}

	public void setOrderItemSet(Set<OrderItem> orderItemSet) {
		this.orderItemSet = orderItemSet;
	}

	@OneToMany(mappedBy = "goods", fetch = FetchType.LAZY)
	public Set<DeliveryItem> getDeliveryItemSet() {
		return deliveryItemSet;
	}

	public void setDeliveryItemSet(Set<DeliveryItem> deliveryItemSet) {
		this.deliveryItemSet = deliveryItemSet;
	}
	// @OneToMany(mappedBy="product", fetch=FetchType.LAZY,
	// cascade={javax.persistence.CascadeType.REMOVE})
	// public Set<GoodsNotify> getGoodsNotifySet()
	// {
	// return this.lIlIlllII;
	// }
	//
	// public void setGoodsNotifySet(Set<GoodsNotify> paramSet)
	// {
	// this.lIlIlllII = paramSet;
	// }

	// @Transient
	// public List<SpecificationValue> getSpecificationValueList()
	// {
	// if (StringUtils.isEmpty(this.specificationValueStore))
	// return null;
	// try
	// {
	// return
	// (List)SystemConfigUtil.getPriceScaleBigDecimal(this.specificationValueStore,
	// new IIIlIlII(this));
	// }
	// catch (Exception localException)
	// {
	// }
	// return null;
	// }
	@ManyToMany(fetch = FetchType.LAZY)
	/**
	 * @return the specificationSet
	 */
	public Set<Specification> getSpecificationSet() {
		return specificationSet;
	}

	/**
	 * @param specificationSet
	 *            the specificationSet to set
	 */
	public void setSpecificationSet(Set<Specification> specificationSet) {
		this.specificationSet = specificationSet;
	}

	@Transient
	public void setSpecificationValueList(List<SpecificationValue> paramList) {
		if ((paramList == null) || (paramList.size() == 0))
			return;
		Collections.sort(paramList);
		// this.specificationValueStore = IIIIIlII.IIIIIIIl(paramList);
	}

	@Transient
	public void onSave() {
		if ((this.price == null)
				|| (this.price.compareTo(new BigDecimal(0)) < 0))
			this.price = new BigDecimal(0);
		if ((this.cost != null) && (this.cost.compareTo(new BigDecimal(0)) < 0))
			this.cost = new BigDecimal(0);
		if ((this.marketPrice == null)
				|| (this.marketPrice.compareTo(new BigDecimal(0)) < 0))
			this.marketPrice = SystemConfigUtil
					.getPriceScaleBigDecimal(this.price);
		if ((this.weight == null) || (this.weight.intValue() < 0))
			this.weight = Integer.valueOf(0);
		if ((this.store != null) && (this.store.intValue() < 0))
			this.store = Integer.valueOf(0);
		if (this.freezeStore.intValue() < 0)
			this.freezeStore = Integer.valueOf(0);
		if (this.isMarketable == null)
			this.isMarketable = Boolean.valueOf(false);
		if (this.isDefault == null)
			this.isDefault = Boolean.valueOf(false);
		if (StringUtils.isEmpty(this.goodsSn))
			this.goodsSn = SerialNumberUtil.buildGoodsSn();
		this.freezeStore = Integer.valueOf(0);
	}

	@Transient
	public void onUpdate() {
		if ((this.price == null)
				|| (this.price.compareTo(new BigDecimal(0)) < 0))
			this.price = new BigDecimal(0);
		if ((this.cost != null) && (this.cost.compareTo(new BigDecimal(0)) < 0))
			this.cost = new BigDecimal(0);
		if ((this.marketPrice == null)
				|| (this.marketPrice.compareTo(new BigDecimal(0)) < 0))
			this.marketPrice = SystemConfigUtil
					.getPriceScaleBigDecimal(this.price);
		if ((this.weight == null) || (this.weight.intValue() < 0))
			this.weight = Integer.valueOf(0);
		if ((this.store != null) && (this.store.intValue() < 0))
			this.store = Integer.valueOf(0);
		if (this.freezeStore.intValue() < 0)
			this.freezeStore = Integer.valueOf(0);
		if (this.isMarketable == null)
			this.isMarketable = Boolean.valueOf(false);
		if (this.isDefault == null)
			this.isDefault = Boolean.valueOf(false);
		if (StringUtils.isEmpty(this.goodsSn))
			this.goodsSn = SerialNumberUtil.buildGoodsSn();
	}

	@Transient
	public Boolean getIsOutOfStock() {
		if ((this.store != null)
				&& (this.freezeStore.intValue() >= this.store.intValue()))
			return Boolean.valueOf(true);
		return Boolean.valueOf(false);
	}

	@Transient
	public BigDecimal getPreferentialPrice(Member paramMember) {
		if (paramMember != null) {
			BigDecimal localBigDecimal = this.price.multiply(new BigDecimal(
					paramMember.getMemberRank().getPreferentialScale()
							.toString()).divide(new BigDecimal(100)));
			localBigDecimal = SystemConfigUtil
					.getPriceScaleBigDecimal(localBigDecimal);
			return localBigDecimal;
		}
		return this.price;
	}
}

/*
 * Location: D:\apache-tomcat-7.0.35\webapps\ROOT1\WEB-INF\classes\ Qualified
 * Name: net.shopxx.entity.Product JD-Core Version: 0.6.2
 */